package com.lieve.servicea.controller;

import com.lieve.servicea.config.DistributedRedisLock;
import com.lieve.servicea.service.ArticleService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;

/**
 * @author sunlijiang
 * @date 2020/1/4
 */
@RestController
@RequestMapping
public class LockController {

    @Resource
    private ArticleService articleService;

    @RequestMapping("/redis")
    public String lock() {
        String lockName = "sunlijiang";
        DistributedRedisLock.acquire(lockName);
        DistributedRedisLock.release(lockName);
        return lockName;
    }



    @RequestMapping("/index")
    public String resposne() {
        Jedis jedis = new Jedis("localhost");
        String value = jedis.get("index");
        jedis.incr("index");
        if (StringUtils.isEmpty(value)) {
            value = "1";
        }
        jedis.set("index", value + 1);
        return value;
    }

    @GetMapping(value = "/hello")
    public String hello() {
        articleService.getArticleList();
        return "Hello";
    }
}
